﻿using ArtMath.Component.Line;
using ArtMath.Component;
using ArtMath.Core.Helper;
using System;
using ArtMath.Attribute;

namespace ArtMath.Parameter
{
    /// <summary>
    /// 两直线夹角
    /// </summary>
    [Serializable]
    public class TwoStraightLinesIncludedAngleParameter : CalculatedParameter
    {
        StraightLineBase line1, line2;
        public TwoStraightLinesIncludedAngleParameter(SketchPad pad, string name, StraightLineBase l1, StraightLineBase l2) : base(pad, name, ToolTypeName.lzxjj)
        {
            Unit = ParameterUnit.Radian;
            StraightLine1 = l1;
            StraightLine2 = l2;
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("{0}1", "StraightLine")]
        public StraightLineBase StraightLine1
        {
            get { return line1; }
            set
            {
                if (line1 is object)
                {
                    line1.CalculatedParameters.Remove(this);
                }
                line1 = value;
                if (line1 is object)
                {
                    line1.CalculatedParameters.Add(this);
                    Value.SetExpression(GetNewValue());
                }
            }
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("{0}2", "StraightLine")]
        public StraightLineBase StraightLine2
        {
            get { return line2; }
            set
            {
                if (line2 is object)
                {
                    line2.CalculatedParameters.Remove(this);
                }
                line2 = value;
                if (line2 is object)
                {
                    line2.CalculatedParameters.Add(this);
                    Value.SetExpression(GetNewValue());
                }
            }
        }
        protected override void ForEachRelatedObject(Predicate<Base> action)
        {
            if (action == null) return;
            if (!action(StraightLine1)) return;
            if (!action(StraightLine2)) return;
        }
        public override double GetNewValue()
        {
            if (StraightLine1 is null || StraightLine2 is null)
            {
                return 0;
            }
            return Utility.GetIncludedAngle(StraightLine1.NewSlope, StraightLine2.NewSlope);
        }
        public override void Delete(bool recoverable = false)
        {
            base.Delete(recoverable);
            if (recoverable)
            {

            }
            else
            {
                StraightLine1 = null;
                StraightLine2 = null;
            }
        }
    }
}
